<?php

require_once "../include/gallery/galleryBuilder.php";
require_once "../include/connectionHandlers/database/Image.php";
require_once "../include/connectionHandlers/database/ImageEvent.php";
require_once "../include/connectionHandlers/flickrHandling.php";
require_once "../include/connectionHandlers/folderHandling.php";
require_once "../include/connectionHandlers/sessionHandling.php";
require_once "../include/connectionHandlers/database/Event.php";

class addPhoto_controller
{
	private $sesHandler = null;
	
	public function __construct()
	{
		$sesHandler = new sessionHandling();
		if(isset($_POST["addPhotoToEvent"]))
		{
			if(isset($_POST["addPhotoToEvent"])){ $this->savePhotos();}
			else{$this->displayEventMissing();}
		}
	}
	
	public function displayEventDropDownList()
	{
		echo "<select name='currentEvent' id='currentEvent'>";
		echo "<option value='' selected='selected'>Select an event</option>";
		$event = new database\Event();
		$eventList = $event->selectAll();
		foreach($eventList as $key=>$value)
		{
			echo "<option value='".$key."'>".$value['event_name']."</option>";
		}
		echo "</select>";
	}
	
	/**
	* Display the gallery of Images from the server folder
	**/
	public function displayPhotosFromFolder()
	{
		$folderHandler = new folderHandling();
		$folderList = $folderHandler->getSets("127.0.0.1");
		if($folderList != null)
		{
			foreach($folderList as $value)
			{
				$title = $value;
				$photoArray = $folderHandler->getPhotosForSet($value);
		
				if($photoArray != null && count($photoArray)>0 && $title!= null)
				{
					echo "<div class='folderPhotos'>";
					$galleryBuilder = new galleryBuilder($title, $photoArray, "SMALL");
					$galleryBuilder->setSelectable();
					$galleryBuilder->printGalleryInList();
					echo "</div>";
				}
			}	
		}
	}
	
		/**
	* Display the gallery of Images in Database with a Title and description text box
	**/
	public function displayPhotosFromFlickr()
	{
		$photosFromFlickr = $this->sesHandler->getPhotosFromFlickr();
		if ($photosFromFlickr!= null)
		{
			foreach($photosFromFlickr as $key=>$value)
			{
				if($value != null && count($value)>0 && $key!= null)
				{
					echo "<div>";
					$galleryBuilder = new galleryBuilder($key, $value, "SMALL");
					$galleryBuilder->setSelectable();
					$galleryBuilder->printGalleryInList();
					echo "</div>";
				}
			}
		}
	}
	
			/**
	* Display the gallery of Images in Database with a Title and description text box
	**/
	public function displayPhotosFromPicasa()
	{

	}
	
	/**
	* Display the gallery of Images in Database with a Title and description text box
	**/
	public function displayPhotosFromFacebook()
	{

	}
	
	/**
	*  Save the description (title and text) entered
	*  $_POST["desciptionText"] &  $_POST["desciptionTitle"]
	**/
	private function savePhotos()
	{
		
		if(isset($_POST["currentEvent"]))
		{
			$currentEvent = $_POST["currentEvent"];
			if($currentEvent!= null && $currentEvent != "")
			{
				$imageArray = $_POST["imagesArray"];
				$images = explode(",",$imageArray);
				foreach($images as $value)
				{
					$imageToStore = new Image($value, false);
					$image = new database\Image();
					$imageEvent = new database\ImageEvent();
					$imageId = $image->insert($imageToStore->getImagePath());
					$imageEvent->insert(array($imageId, $currentEvent));
				}
			}
		}
	}
		
	private function displayEventMissing()
	{
	
	}
	
	private function loadImagesFromFlickr()
	{
		if(isset($_POST["flickr_email"])){
			$flickrEmail = $_POST["flickr_email"];
			$flickrHandler = new filckrHandling();
			$setList = $flickrHandler->getSets($flickrEmail);
			if ($setList != null){
				foreach($setList as $key=>$value)
				{
					$title = $key;
					$setId = $value;
			
					$responsePhoto = $flickrHandler->getSmallPhotosForSet($setId);
					if ($responsePhoto != null)
					{
						$sesHandler->addPhotosFromFlickr($title, $responsePhoto);
					}
				}
			}
		}
	}

}
?>